﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:user
	/// </summary>
	public partial class user
	{
		public user()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("user_id", "user"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int user_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [user]");
			strSql.Append(" where user_id=@user_id");
			SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)
			};
			parameters[0].Value = user_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcs.Model.user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [user](");
            strSql.Append("[user_name],user_login,user_pass,user_mobelPhone,user_phone,user_fax,user_company,user_email,roles_id,remark,sid,user_depid,user_aid,dogid)");
			strSql.Append(" values (");
            strSql.Append("@user_name,@user_login,@user_pass,@user_mobelPhone,@user_phone,@user_fax,@user_company,@user_email,@roles_id,@remark,@sid,@user_depid,@user_aid,@dogid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@user_name", SqlDbType.VarChar,20),
					new SqlParameter("@user_login", SqlDbType.VarChar,30),
					new SqlParameter("@user_pass", SqlDbType.VarChar,30),
					new SqlParameter("@user_mobelPhone", SqlDbType.VarChar,30),
					new SqlParameter("@user_phone", SqlDbType.VarChar,30),
					new SqlParameter("@user_fax", SqlDbType.VarChar,30),
					new SqlParameter("@user_company", SqlDbType.VarChar,30),
					new SqlParameter("@user_email", SqlDbType.VarChar,30),
					new SqlParameter("@roles_id", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.VarChar,300),
					new SqlParameter("@sid", SqlDbType.Int,4),
                                        new SqlParameter("@user_depid", SqlDbType.Int,4),
                                        new SqlParameter("@user_aid", SqlDbType.Int,4),
                                        new SqlParameter("@dogid", SqlDbType.NVarChar,30)};
			parameters[0].Value = model.user_name;
			parameters[1].Value = model.user_login;
			parameters[2].Value = model.user_pass;
			parameters[3].Value = model.user_mobelPhone;
			parameters[4].Value = model.user_phone;
			parameters[5].Value = model.user_fax;
			parameters[6].Value = model.user_company;
			parameters[7].Value = model.user_email;
			parameters[8].Value = model.roles_id;
			parameters[9].Value = model.remark;
			parameters[10].Value = model.sid;
            parameters[11].Value = model.user_depid;
            parameters[12].Value = model.user_aid;
            parameters[13].Value = model.dogid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcs.Model.user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [user] set ");
			strSql.Append("[user_name]=@user_name,");
			strSql.Append("user_login=@user_login,");
			strSql.Append("user_pass=@user_pass,");
			strSql.Append("user_mobelPhone=@user_mobelPhone,");
			strSql.Append("user_phone=@user_phone,");
			strSql.Append("user_fax=@user_fax,");
			strSql.Append("user_company=@user_company,");
			strSql.Append("user_email=@user_email,");
			strSql.Append("roles_id=@roles_id,");
			strSql.Append("remark=@remark,");
			strSql.Append("sid=@sid,");
            strSql.Append("user_depid=@user_depid ,");
            strSql.Append("user_aid=@user_aid ,");
            strSql.Append("dogid=@dogid");
			strSql.Append(" where user_id=@user_id");
			SqlParameter[] parameters = {
					new SqlParameter("@user_name", SqlDbType.VarChar,20),
					new SqlParameter("@user_login", SqlDbType.VarChar,30),
					new SqlParameter("@user_pass", SqlDbType.VarChar,30),
					new SqlParameter("@user_mobelPhone", SqlDbType.VarChar,30),
					new SqlParameter("@user_phone", SqlDbType.VarChar,30),
					new SqlParameter("@user_fax", SqlDbType.VarChar,30),
					new SqlParameter("@user_company", SqlDbType.VarChar,30),
					new SqlParameter("@user_email", SqlDbType.VarChar,30),
					new SqlParameter("@roles_id", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.VarChar,300),
					new SqlParameter("@sid", SqlDbType.Int,4),
                     new SqlParameter("@user_depid", SqlDbType.Int,4),
                      new SqlParameter("@user_aid", SqlDbType.Int,4),
                       new SqlParameter("@dogid", SqlDbType.VarChar,200),
                    new SqlParameter("@user_id", SqlDbType.Int,4)};
			parameters[0].Value = model.user_name;
			parameters[1].Value = model.user_login;
			parameters[2].Value = model.user_pass;
			parameters[3].Value = model.user_mobelPhone;
			parameters[4].Value = model.user_phone;
			parameters[5].Value = model.user_fax;
			parameters[6].Value = model.user_company;
			parameters[7].Value = model.user_email;
			parameters[8].Value = model.roles_id;
			parameters[9].Value = model.remark;
			parameters[10].Value = model.sid;
			parameters[11].Value = model.user_depid;
            parameters[12].Value = model.user_aid;
            parameters[13].Value = model.dogid;
            parameters[14].Value = model.user_id;
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int user_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [user] ");
			strSql.Append(" where user_id=@user_id");
			SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)
			};
			parameters[0].Value = user_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string user_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from user ");
			strSql.Append(" where user_id in ("+user_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.user GetModel(int user_id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 user_id,[user_name],user_login,user_pass,user_mobelPhone,user_phone,user_fax,user_company,user_email,roles_id,remark,sid,user_depid,user_aid,dogid from [user] ");
			strSql.Append(" where user_id=@user_id");
			SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4)
			};
			parameters[0].Value = user_id;

			lcs.Model.user model=new lcs.Model.user();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.user DataRowToModel(DataRow row)
		{
			lcs.Model.user model=new lcs.Model.user();
			if (row != null)
			{
				if(row["user_id"]!=null && row["user_id"].ToString()!="")
				{
					model.user_id=int.Parse(row["user_id"].ToString());
				}
				if(row["user_name"]!=null)
				{
					model.user_name=row["user_name"].ToString();
				}
				if(row["user_login"]!=null)
				{
					model.user_login=row["user_login"].ToString();
				}
				if(row["user_pass"]!=null)
				{
					model.user_pass=row["user_pass"].ToString();
				}
				if(row["user_mobelPhone"]!=null)
				{
					model.user_mobelPhone=row["user_mobelPhone"].ToString();
				}
				if(row["user_phone"]!=null)
				{
					model.user_phone=row["user_phone"].ToString();
				}
				if(row["user_fax"]!=null)
				{
					model.user_fax=row["user_fax"].ToString();
				}
				if(row["user_company"]!=null)
				{
					model.user_company=row["user_company"].ToString();
				}
				if(row["user_email"]!=null)
				{
					model.user_email=row["user_email"].ToString();
				}
				if(row["roles_id"]!=null && row["roles_id"].ToString()!="")
				{
					model.roles_id=int.Parse(row["roles_id"].ToString());
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
				if(row["sid"]!=null && row["sid"].ToString()!="")
				{
					model.sid=int.Parse(row["sid"].ToString());
				}
                if (row["user_depid"] != null && row["user_depid"].ToString() != "")
                {
                    model.user_depid = int.Parse(row["user_depid"].ToString());
                }
                if (row["user_aid"] != null && row["user_aid"].ToString() != "")
                {
                    model.user_aid = int.Parse(row["user_aid"].ToString());
                }
                if (row["dogid"] != null)
                {
                    model.dogid = row["dogid"].ToString();
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select user_id,[user_name],user_login,user_pass,user_mobelPhone,user_phone,user_fax,user_company,user_email,roles_id,remark,sid,[user_depid],[depname],user_aid,aname,abm,dogid ");
            strSql.Append(" FROM [VuserRoles] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
       

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" user_id,user_name,user_login,user_pass,user_mobelPhone,user_phone,user_fax,user_company,user_email,roles_id,remark,sid ");
			strSql.Append(" FROM user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.user_id desc");
			}
			strSql.Append(")AS Row, T.*  from user T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "user";
			parameters[1].Value = "user_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 得到材料员审核人对应表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetUserCheck(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VuserCheck ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 得到用户角色视图
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetUserRoles(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VuserRoles ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到材料员项目视图
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetUserProject(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct proid,projectname ");
            strSql.Append(" FROM VuserMaterialInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		#endregion  ExtensionMethod
	}
}

